//
//  DEWChangeVRingView.m
//  OtherDemo
//
//  Created by kangbinbin on 2018/7/25.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWChangeVRingView.h"
#import "ButtonCollectionCell.h"

@interface DEWChangeVRingView()<UICollectionViewDelegate,UICollectionViewDataSource>
/** <#type#> */
@property (strong, nonatomic) UICollectionViewFlowLayout *layout;
/** <#type#> */
@property (strong, nonatomic) UICollectionView  *collectView;

/** <#type#> */
@property (strong, nonatomic) UIView *AlertView;


@end

@implementation DEWChangeVRingView

+ (DEWChangeVRingView *)showChangeRingViewDataArr:(NSMutableArray *)Arr{
    DEWChangeVRingView *changeRing = [[DEWChangeVRingView alloc]initWithDataArr:Arr];
    
    return changeRing;
    
}
- (instancetype)initWithDataArr:(NSMutableArray *)arr{
    if (self = [super init]) {
        
        [self initUI];
        
        [self show];
        
    }
    return self;
}

-(void)initUI{
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    self.frame = CGRectMake(0, 0, DEWScreenWidth, DEWScreenHeight);
    
    UIView *AlertView = [[UIView alloc]initWithFrame:CGRectMake(23, 109, DEWScreenWidth - 46 , DEWScreenHeight - 109 * 2)];
    AlertView.backgroundColor = [UIColor whiteColor];
    AlertView.layer.cornerRadius = 10;
    [self addSubview:AlertView];
    self.AlertView = AlertView;
    
    UILabel *titleL = [[UILabel alloc]init];
    titleL.frame = CGRectMake(0, 30, AlertView.width, 27);
    titleL.text = @"选择你要关注的圈子";
    titleL.textColor = UIColorFromRGB(0x333333);
    titleL.font = [UIFont systemFontOfSize:19.f];
    titleL.textAlignment = NSTextAlignmentCenter;
    [AlertView addSubview:titleL];
    
    UILabel *subL = [[UILabel alloc]init];
    subL.frame = CGRectMake(0, titleL.bottom + 8, AlertView.width, 15);
    subL.text = @"为了与邻居更好的交流，请最少选择4个";
    subL.textColor = UIColorFromRGB(0x999999);
    subL.textAlignment = NSTextAlignmentCenter;
    subL.font = [UIFont systemFontOfSize:11.f];
    [AlertView addSubview:subL];
    
   
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.size = CGSizeMake(30, 30);
    cancelBtn.centerX = self.centerX;
    cancelBtn.y = AlertView.bottom + 20;
    [cancelBtn setImage:[UIImage imageNamed:@"关闭弹窗"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;///滑动方向
    _layout.itemSize = CGSizeMake((DEWScreenWidth - 50)/4, 100);
    _layout.minimumLineSpacing = 0;///行距
    _layout.minimumInteritemSpacing = 0;///间距
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);///cell呈现区域边界边距
    
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, subL.bottom + 5, DEWScreenWidth - 46, AlertView.height - 60  - 80) collectionViewLayout:_layout];
    _collectView.scrollEnabled = NO;
    _collectView.backgroundColor = [UIColor whiteColor];
    
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [AlertView addSubview:_collectView];
    
    ///注册
    [self.collectView registerClass:[ButtonCollectionCell class] forCellWithReuseIdentifier:@"collectionID"];
    
    UIButton *quedingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [AlertView addSubview:quedingBtn];
    quedingBtn.frame = CGRectMake(25,_collectView.bottom, AlertView.width - 50, 40);
    [quedingBtn setTitle:@"确定" forState:UIControlStateNormal];
    [quedingBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    [quedingBtn setBackgroundColor:UIColorFromRGB(0xFF8900)];
    [quedingBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    quedingBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    quedingBtn.layer.cornerRadius = 4;
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ButtonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionID" forIndexPath:indexPath];
    
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(((DEWScreenWidth - 50)/4 - 50) / 2, 8, 50, 50)];
    imgView.image = [UIImage imageNamed:@"我的老乡"];
    [cell addSubview:imgView];
    
    UIImageView *selectedImg = [[UIImageView alloc]initWithFrame:CGRectMake(imgView.right - 10, imgView.bottom - 10, 10, 10)];
    selectedImg.image = [UIImage imageNamed:@"等级"];
    [cell addSubview:selectedImg];
    
    UILabel *subL = [[UILabel alloc]init];
    subL.frame = CGRectMake(0, imgView.bottom + 8,  (DEWScreenWidth - 46) /4, 17);
    subL.text = @"测试数据";
    subL.textColor = UIColorFromRGB(333333);
    subL.font = [UIFont systemFontOfSize:12];
    subL.textAlignment = NSTextAlignmentCenter;
    [cell addSubview:subL];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 
}

#pragma mark - 确定按钮点击事件
-(void)buttonClick:(UIButton *)sender{
    [self closeViewTime:0.5];
}

-(void)closeViewTime:(NSTimeInterval)duration{
    [UIView animateWithDuration:duration animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
-(void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.AlertView.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.AlertView.alpha = 1;
    }];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:self];
    if (!CGRectContainsPoint([self.AlertView frame], pt)) {
        [self closeViewTime:0.5];
    }
    
}

@end
